function loadAnswers(questionId) {
    jQuery.get(encodeURI('/getanswers?cid=' + questionId), null,
        function (data) {
             commentsList.html(data)
        }, 'html');
}

function postQuestion() {
    var value = questionEditor.textarea.val();
    if (jQuery.trim(value).length == 0) {
        alert('Your question must not be empty!');
        return
    }
    var cid = questionEditor.tid;
    var path = '/questions';
    if (cid != null) {
        path += '/' + cid
    }
    jQuery.post(path, value,
        function (data) {
            mainFrame.query.val('');
            questionEditor.jqmHide();
            questionEditor.cleanup();
            if (cid == null) {
                loadQuestionsPage(1);
            }
            else {
                var prev = questionsList.getSelected();
                prev.setContent(value);
                const height = getExcerptHeight(prev) + 'px';
                prev.parent().css('height', height)
            }
        }, 'html');
}

function voteQuestionUp(question) {
    var cid = question.getId();
    jQuery.post('/questions/voteup?cid=' + cid, null,
        function (resp) {
            var votes = question.find('.votes span');
            votes.text(parseInt(resp));
            question.find('input[name=aperms]').val('00');
            questionToolbar.voteUpBtn.hide();
            questionToolbar.voteDownBtn.hide();
        }, 'html')
}

function voteQuestionDown(question) {
    var cid = question.getId();
    jQuery.post('/questions/votedown?cid=' + cid, null,
        function (resp) {
            var votes = question.find('.votes span');
            votes.text(parseInt(resp));
            question.find('input[name=aperms]').val('00');
            questionToolbar.voteUpBtn.hide();
            questionToolbar.voteDownBtn.hide();
        }, 'html')
}

function voteAnswerUp(answer) {
    var cid = answer.getId();
    jQuery.post('/answers/voteup?cid=' + cid, null,
        function (resp) {
            var votes = answer.find('span[name="votes"]');
            votes.text(parseInt(resp));
            answer.find('input[name=aperms]').val('00');
            answerToolbar.voteUpBtn.hide();
            answerToolbar.voteDownBtn.hide();
        }, 'html')
}

function voteAnswerDown(answer) {
    var cid = answer.getId();
    jQuery.post('/answers/votedown?cid=' + cid, null,
        function (resp) {
            var votes = answer.find('span[name="votes"]');
            votes.text(parseInt(resp));
            answer.find('input[name=aperms]').val('00');
            answerToolbar.voteUpBtn.hide();
            answerToolbar.voteDownBtn.hide();
        }, 'html')
}

function postAnswer() {
    if (answerEditor.isEmpty()) {
        alert('Your answer must not be empty!');
        return
    }
    var aid = answerEditor.tid;
    var value = answerEditor.textarea.val();
    if (aid != null) {
        jQuery.post('/answers/' + aid, value,
            function () {
                var answer = commentsList.getAnswer(aid);
                answer.setContent(value);
                answerEditor.cleanup()
            }, 'html');
    }
    else {
        var question = $('#content .selected');
        var qid = question.getId();
        jQuery.post('/addanswer?cid=' + qid, value,
            function (data) {
                answerEditor.cleanup();
                commentsList.html(data)
            }, 'html');
    }
}

function loadQuestionsPage(pageNum) {
    answerEditor.hide();
    dock.append(answerEditor);
    questionToolbar.detach();
    var query = mainFrame.query.val();
    var url = '/search?query={0}&page={1}'.format(query, pageNum);
    mainFrame.query.focus();
    questionsList.load(encodeURI(url), null,
        function () {
            $.scrollTo(0, animSpeed, {queue: true, axis: 'y'});
        });
}

String.prototype.format = function () {
    var args = arguments;
    return this.replace(/{(\d+)}/g, function (match, number) {
        return typeof args[number] != 'undefined'
            ? args[number]
            : match;
    });
};

function log(msg) {
    console.log(msg)
}